﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
using System.Text;

namespace PMS.Web
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PMS_User userInfo = PageComm.GetUser(Response);
                hid_UserID.Value = userInfo.id.ToString();
                LabelLoginName.Text = userInfo.loginName;
                if (userInfo.role == "管理员")
                {
                    lbtn_MenuMaintain.Visible = true;
                }
                else
                {
                    lbtn_MenuMaintain.Visible = false;
                }
                DataBindMenu();
            }
        }

        public void DataBindMenu()
        {
            List<PMS_RoleList> list = PageComm.GetCacheList();
            StringBuilder menuStr = new StringBuilder();
            foreach (PMS_RoleList m1 in list.Where(p => p.parentID == 0).OrderBy(p=>p.roleText))
            {
                menuStr.Append("<li roleid=" + m1.roleID + "><a href=\"javascript:void(0);\">" + m1.roleText + "</a>");
                menuStr.Append(GetLsit(list, m1));
                menuStr.Append("</li>");
            }
            leftMenu.InnerHtml = menuStr.ToString();
        }

        public string GetLsit(IEnumerable<PMS_RoleList> list, PMS_RoleList m)
        {
            StringBuilder menuStr = new StringBuilder();
            IEnumerable<PMS_RoleList> list1 = list.Where(p => p.parentID == m.roleID).OrderBy(p => p.roleText);
            if (list1.Count() > 0)
            {
                menuStr.Append("<ul class=\"child\">");
            }
            foreach (PMS_RoleList m1 in list1)
            {
                menuStr.Append("<li roleid=" + m1.roleID + "><a href=\"javascript:void(0);\">" + m1.roleText + "</a>");
                menuStr.Append(GetLsit(list, m1));
            }
            if (list1.Count() > 0)
            {
                menuStr.Append("</ul>");
            }
            return menuStr.ToString();
        }

        protected void ButtonLogout_Click(object sender, EventArgs e)
        {
            Session["UserInfo"] = null;
            Session.RemoveAll();
            Response.Redirect("Login.aspx");
        }

        protected void lbtn_MenuMaintain_Click(object sender, EventArgs e)
        {
            Response.Redirect("MenuMaintain.aspx");
        }

        protected void lbtn_Return_Click(object sender, EventArgs e)
        {
            Response.Redirect("ProjectMaintain.aspx");
        }
    }
}
